package controllers;

import play.data.validation.*;
import play.*;
import play.mvc.*;
import java.util.*;

import models.*;
import java.io.IOException;
import java.util.Calendar;
import com.ebay.sdk.ApiContext;
import com.ebay.sdk.ApiCredential;
import com.ebay.sdk.ApiException;
import com.ebay.sdk.SdkException;
import com.ebay.sdk.call.AddFixedPriceItemCall;
import com.ebay.sdk.call.AddItemCall;
import com.ebay.sdk.call.EndItemCall;
import com.ebay.sdk.call.GetItemCall;
import com.ebay.sdk.call.GetProductsCall;
import com.ebay.sdk.call.GetSellerListCall;
import com.ebay.sdk.call.GeteBayOfficialTimeCall;
import com.ebay.sdk.util.eBayUtil;
import com.ebay.sdk.TimeFilter;
import com.ebay.services.finding.SearchItem;
import com.ebay.soap.eBLBaseComponents.AmountType;
import com.ebay.soap.eBLBaseComponents.BuyerPaymentMethodCodeType;
import com.ebay.soap.eBLBaseComponents.CatalogProductType;
import com.ebay.soap.eBLBaseComponents.CategoryType;
import com.ebay.soap.eBLBaseComponents.CountryCodeType;
import com.ebay.soap.eBLBaseComponents.CurrencyCodeType;
import com.ebay.soap.eBLBaseComponents.DetailLevelCodeType;
import com.ebay.soap.eBLBaseComponents.EndReasonCodeType;
import com.ebay.soap.eBLBaseComponents.ExternalProductCodeType;
import com.ebay.soap.eBLBaseComponents.ExternalProductIDType;
import com.ebay.soap.eBLBaseComponents.FeesType;
import com.ebay.soap.eBLBaseComponents.ItemType;
import com.ebay.soap.eBLBaseComponents.ListingDurationCodeType;
import com.ebay.soap.eBLBaseComponents.ListingTypeCodeType;
import com.ebay.soap.eBLBaseComponents.ProductListingDetailsType;
import com.ebay.soap.eBLBaseComponents.ProductSearchType;
import com.ebay.soap.eBLBaseComponents.ReturnPolicyType;
import com.ebay.soap.eBLBaseComponents.ShippingDetailsType;
import com.ebay.soap.eBLBaseComponents.ShippingServiceCodeType;
import com.ebay.soap.eBLBaseComponents.ShippingServiceOptionsType;
import com.ebay.soap.eBLBaseComponents.ShippingTypeCodeType;
import java.util.Locale;

@With(Secure.class)
public class Seller extends Controller{
		
	public static void index() throws ApiException, SdkException, Exception{
		ItemType[] items = null;
		Api myApi = new Api();
		items = myApi.getAllCurrentItems();
		ArrayList itemList = new ArrayList();
		for(int i=0;i<items.length;i++){
			itemList.add(myApi.viewItem(items[i].getItemID()));
		}
		String user = Security.connected();
		render(user, itemList);
	}
	
	public static void addItem(@Required String title, @Required String isbn, @Required String description, @Required double itemPrice, @Required double shippingPrice, @Required int duration) throws ApiException, SdkException, Exception{
		List<SearchItem> searchItems=null;
		
		
		Api myApi = new Api();
		//searchItems=myApi.buyerSearchByKeywordFindingApi(title);
		String user = Security.connected();
		/*if(myApi.buyerSearchByKeywordFindingApi(title)==null){
			flash.error("you cannot add an duplicated item"); 
			render(user);
		}*/
		
		if(validation.hasErrors()) {
            flash.error("please enter full item information!");            
            render(user);
        }
		ListingDurationCodeType dura = ListingDurationCodeType.DAYS_1;
		
		switch (duration){
		case 1: dura = ListingDurationCodeType.DAYS_3;break;
		case 2: dura = ListingDurationCodeType.DAYS_10;break;
		case 3: dura = ListingDurationCodeType.DAYS_30;break;
		default: break;
		}
		try {
			String id = myApi.addItem(title, isbn, description, itemPrice, shippingPrice,dura);
		} catch (ApiException e) {
			failurePage(e.getMessage());
		}
		
		confirmPage("Your item is now listed on eBay.");
		
	}
	public static void failurePage(String failureMessage){
		render(failureMessage);
	}
	public static void confirmPage(String confirmMessage){
		render(confirmMessage);
	}
	public static void removeItem(String itemIDStr, int reason) throws ApiException, SdkException, Exception{
		Api myApi = new Api();
		String user = Security.connected();
		EndReasonCodeType endReasonCodeType = EndReasonCodeType.LOST_OR_BROKEN;
		switch(reason){
		case 1: endReasonCodeType = EndReasonCodeType.INCORRECT;break;
		case 2: endReasonCodeType = EndReasonCodeType.LOST_OR_BROKEN;break;
		case 3: endReasonCodeType = EndReasonCodeType.NOT_AVAILABLE;break;
		case 4: endReasonCodeType = EndReasonCodeType.OTHER_LISTING_ERROR;break;
		case 5: endReasonCodeType = EndReasonCodeType.SELL_TO_HIGH_BIDDER;break;
		case 6: endReasonCodeType = EndReasonCodeType.SOLD;break;
		default: break;
		}
		
		try {
			myApi.removeItem(itemIDStr, endReasonCodeType);
		} catch (ApiException a) {
			boolean alreadyClosed = true;
			render(user, alreadyClosed);
		}
		confirmPage("Your item listing has been deleted.");
		//render(user);
	} 
	public static void deleteItem(){
		String user = Security.connected();
		render(user);
	}
	
	public static void editItem(String itemIDStr) throws ApiException, SdkException, Exception{
		Api myApi = new Api();
		ItemType item = myApi.viewItem(itemIDStr);		
		String user = Security.connected();
		render(user, item);
	}
	
	public static void updateItem(@Required String itemIDStr,@Required String title, @Required String isbn, @Required String description, @Required double itemPrice, @Required double shippingPrice, @Required int duration) throws ApiException, SdkException, Exception{
		String user = Security.connected();
		
		if(validation.hasErrors()) {
            flash.error("Please enter full item information.");            
            renderTemplate("seller/editItem.html");
            //render(user);
        }
		Api myApi = new Api();
		myApi.removeItem(itemIDStr, EndReasonCodeType.OTHER_LISTING_ERROR);
				
		ListingDurationCodeType dura = ListingDurationCodeType.DAYS_1;
		
		switch (duration){
		case 1: dura = ListingDurationCodeType.DAYS_3;break;
		case 2: dura = ListingDurationCodeType.DAYS_10;break;
		case 3: dura = ListingDurationCodeType.DAYS_30;break;
		default: break;
		}
		String id = myApi.addItem(title, isbn, description, itemPrice, shippingPrice, dura);
		confirmPage("Your item listing has been updated.");
		//index();
	}
}
